using System;
using Microsoft.SharePoint;

namespace RoketSolutions.SP2010.Infrostructure.Security
{
    public static class AdminContext
    {
        public static void Do(SPSite site, Action<SPSite, SPWeb> operation)
        {
            using (var context = new AdminContextScope(site))
            {
                context.Do(operation);
            }
        }

        public static void Do(SPWeb web, Action<SPSite, SPWeb> operation)
        {
            using (var context = new AdminContextScope(web))
            {
                context.Do(operation);
            }
        }

        public static void Do(string webUrl, Action<SPSite, SPWeb> operation)
        {
            using (var context = new AdminContextScope(webUrl))
            {
                context.Do(operation);
            }
        }

        public static void Do(ISecurableObject securable, Action<SPSite, SPWeb> operation)
        {
            using (var context = new AdminContextScope(ResolveWeb(securable)))
            {
                context.Do(operation);
            }
        }

        private static SPWeb ResolveWeb(ISecurableObject securable)
        {
            if (securable == null)
                throw new ArgumentNullException("securable", "To resolve web from securable object provide object");

            if (securable is SPListItem)
                return ((SPListItem)securable).Web;

            if (securable is SPList)
                return ((SPList)securable).ParentWeb;

            if (securable is SPWeb)
                return (SPWeb)securable;

            throw new ArgumentException("Unknown securable object", "securable");
        }
    }

    public class AdminContextScope : IDisposable
    {
        #region / Fields /

        private bool _isSetup;
        private string _webUrl;

        #endregion

        #region / Constructors /

        public AdminContextScope(SPSite site)
        {
            OriginalSite = site;
            OriginalWeb = site.RootWeb;
        }

        public AdminContextScope(SPWeb web)
        {
            OriginalSite = web.Site;
            OriginalWeb = web;
        }

        public AdminContextScope(string webUrl)
        {
            this._webUrl = webUrl;
        }

        #endregion

        #region / Properties /

        public SPSite Site { get; private set; }

        public SPWeb Web { get; private set; }

        private SPSite OriginalSite { get; set; }

        private SPWeb OriginalWeb { get; set; }

        #endregion

        #region / Methods /

        public void Do(Action<SPSite, SPWeb> operation)
        {
            SPSecurity.RunWithElevatedPrivileges(
                () =>
                    {
                        Setup();
                        operation(Site, Web);
                    });
        }

        public void Dispose()
        {
            Reset();
        }

        public void Setup()
        {
            if (!_isSetup)
            {
                _isSetup = true;

                if (OriginalSite != null && OriginalWeb != null)
                {
                    Site = new SPSite(OriginalSite.ID, OriginalSite.Zone);
                    Web = Site.OpenWeb(OriginalWeb.ID);
                }
                else if (!String.IsNullOrEmpty(_webUrl))
                {
                    Site = new SPSite(_webUrl);
                    Web = Site.OpenWeb();
                }
            }
        }

        public void Reset()
        {
            if (_isSetup)
            {
                _isSetup = false;
                if (Web != null)
                    Web.Dispose();
                if (Site != null)
                    Site.Dispose();
            }
        }

        #endregion
    }
}